<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class UploadController extends Controller
{
    public function uploadfile(Request $request)
    {
        if ($request->hasFile('file') && $request->file('file')->isValid()) {
            $storepath = $request->path;
            if (empty($storepath)) {
                $storepath = 'uploads';
            }
            $path = $request->file('file')->store($storepath, 'uploads');
            $url = env('APP_URL') .'/'. $path;
            return $this->ajaxReturn(['code' => 1, 'msg' => '上传成功！', 'path' => $path, 'url' => $url]);
        }
        return $this->ajaxReturn(['code' => 0, 'msg' => '上传失败！']);
    }

    public function removefile(Request $request)
    {
        $path = $request->path;
        $sart = strpos($path, 'uploads');
        $path = substr($path, $sart, 500);
        unlink($path); // 删除文件
        if (!file_exists($path)) {
            return $this->ajaxReturn(['code' => 1, 'msg' => '删除成功!']);
        } else {
            return $this->ajaxReturn(['code' => 0, 'msg' => '删除失败,文件为空!']);
        }
    }
}